package cn.xd.web.utils;

import cn.hutool.core.util.StrUtil;
import cn.xd.common.constant.AuthConstant;
import cn.xd.common.domain.auth.CurrentAdminUser;
import cn.xd.common.domain.auth.CurrentCustomerUser;
import cn.xd.common.domain.auth.CurrentUser;
import cn.xd.common.enums.auth.UserTypeEnum;
import cn.xd.common.exception.BizException;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

/**
 * @author : mirzaLin
 * @data : 2023/11/21
 * @description : 登录用户信息
 **/
public class SecurityUtils {
    private static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return Objects.requireNonNull(servletRequestAttributes).getRequest();
    }

    /**
     * 获得用户
     *
     * @return
     */
    public static CurrentUser getUser() {
        String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
        if (StrUtil.isEmpty(header)) {
            throw new BizException("未找到用户，请重新登录！");
        }
        return JSON.parseObject(header, CurrentUser.class);
    }

    /**
     * 获得用户
     *
     * @return
     */
    public static CurrentUser getUserNotThrow() {
        String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
        if (StrUtil.isEmpty(header)) {
            return null;
        }
        return JSON.parseObject(header, CurrentUser.class);
    }


    /**
     * 获得adminUser用户
     *
     * @return
     */
    public static CurrentAdminUser getAdminUser() {
        CurrentUser user = getUser();
        if (user.getUserType().equals(UserTypeEnum.ADMIN)) {
            String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
            return JSON.parseObject(header, CurrentAdminUser.class);
        }
        return null;
    }

    /**
     * 获得CustomerUser用户
     *
     * @return
     */
    public static CurrentCustomerUser getCustomerUser() {
        CurrentUser user = getUser();
        if (user.getUserType().equals(UserTypeEnum.ADMIN)) {
            String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
            return JSON.parseObject(header, CurrentCustomerUser.class);
        }
        return null;
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public static Long getUserId() {
        String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
        if (StrUtil.isEmpty(header)) {
            throw new BizException("未找到用户，请重新登录！");
        }
        return JSON.parseObject(header, CurrentUser.class).getId();
    }

    /**
     * 获取管理者用户ID
     *
     * @return
     */
    public static Long getAdminUserId() {
        String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
        if (StrUtil.isEmpty(header)) {
            throw new BizException("未找到用户，请重新登录！");
        }
        return JSON.parseObject(header, CurrentUser.class).getAdminUserId();
    }

    /**
     * 获取使用者用户ID
     *
     * @return
     */
    public static Long getCustomerUserId() {
        String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
        if (StrUtil.isEmpty(header)) {
            throw new BizException("未找到用户，请重新登录！");
        }
        return JSON.parseObject(header, CurrentUser.class).getCustomerUserId();
    }

    /**
     * 获得adminUser  机构id
     *
     * @return
     */
    public static Long getAdminUserOrgId() {
        CurrentUser user = getUser();
        if (user.getUserType().equals(UserTypeEnum.ADMIN)) {
            String header = (String) getRequest().getAttribute(AuthConstant.USER_TOKEN_HEADER);
            return JSON.parseObject(header, CurrentAdminUser.class).getOrgId();
        }
        return null;
    }
}
